﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.OleDb;

namespace DAO
{
    public class PhieuThueChiTietDAO
    {
        DataTable dt = new DataTable();
        OleDbDataAdapter da ;
        public int ThemChiTietPhieuThueDAO(ListChiTietPhieuDTO dto)
        {
            int result =0;
            for(int i =0 ;i<dto.Dsphieu.Count();i++)
            {
                KhachHangDAO dao = new KhachHangDAO();
                dao.ThemKhachHang(dto.Dsphieu[i].Tenkh, dto.Dsphieu[i].Maloaikh, dto.Dsphieu[i].Cmnd, dto.Dsphieu[i].Diachi);
                dto.Dsphieu[i].Makh = dao.GetMaxMaKH();
                result = DataProvider.ExecQueryWithParameters("LapChiTietPhieu", "@MaPhieu", dto.Dsphieu[i].Maphieu, "@MaKH",dto.Dsphieu[i].Makh);
                if(result ==0)
                    return result;
            }
            return result;
        }
        public DataTable loadChiTiet()
        {
            // fill db
            OleDbConnection cn = DataProvider.ConectDatabase();
            da = new OleDbDataAdapter("SELECT * FROM PhieuThuePhongCT",cn);
            dt.Clear();
            da.Fill(dt);
            cn.Close();
            return dt;
            
        }
        public DataTable loadChiTietTheoMaPhieu(int maphieu)
        {
            OleDbConnection cn = DataProvider.ConectDatabase();
            da = new OleDbDataAdapter("SELECT C.MaPhieu,C.MaCT,K.MaKH,K.TenKH,K.MaLoaiKH,K.DiaChi,K.CMND FROM PhieuThuePhongCT as C,KhachHang K Where C.MaPhieu=" + maphieu+" and C.MaKH = K.MaKH", cn);
            dt.Clear();
            da.Fill(dt);
            cn.Close();
            return dt;
        }
        public int XoaCT(int machitiet)
        {
            return DataProvider.intExcuteQueRy("Delete * From PhieuThuePhongCT where MaCT=" + machitiet);

        }
        public int SoluongKhach(int Maphieu)
        {
            DataTable dt =new DataTable();
            try
            {
                dt = DataProvider.LoadTable("select * from PhieuThuePhongCT where MaPhieu =" + Maphieu);
              int kq = dt.Rows.Count;
                return kq;
            }
           catch (Exception ex)
            {
                throw ex;
            }
        }
        public  int TinhTienPhong(PhieuThueChiTietDTO PhieuThue)
        {
            int thanhtien = 0;
            ThamSoDAO daothamso = new ThamSoDAO();
            
            thanhtien = (((TimeSpan)(DateTime.Now - PhieuThue.Ngaythue)).Days) * PhieuThue.Gia;

            if (PhieuThue.Soluongkhach == 3)
            {
                thanhtien = thanhtien + (thanhtien * daothamso.LayThamSo().Phuthu/100);
            }


            if (isKhachNuocNgoai(PhieuThue.Maphieu) == true)
            {
                thanhtien = Convert.ToInt32(thanhtien * daothamso.LayThamSo().Heso);
            }
            return thanhtien;
        }
        public  bool isKhachNuocNgoai(int maPhieuThuePhong)
        {

            try
            {
                DataTable bangketqua = loadChiTietTheoMaPhieu(maPhieuThuePhong);
                if (bangketqua.Rows.Count == 0)
                {
                    return false;
                }
                else
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        if (Int32.Parse(row["MaLoaiKH"].ToString()) == 2)
                            return true;
                    }
                }
                return false;
            }
            catch (Exception e)
            {
                throw e;
            }
        }
       

    }
}
